package com.bear.cmcc.NetTestHTTP.ui.adapter;

import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.ServersConfigBean;
/**
 * ���ط������б��������
 * @author jqa
 *
 */
public class ServerListAdapter extends BaseAdapter {
	private LayoutInflater layoutInflater;
	private List<ServersConfigBean> servList;//�������б�
//	private List<String> servList;
	private int position;
	private Context context;
	public ServerListAdapter(Context context, List<ServersConfigBean> _list) {
		layoutInflater = LayoutInflater.from(context);
		this.servList = _list;
		this.context=context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return servList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return servList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ServersConfigBean servConfig = servList.get(position);
	//	String serName=servList.get(position);
		this.position = position;
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = layoutInflater.inflate(R.layout.layout_servlist_item,
					null);
			holder.tvServName = (TextView) convertView
					.findViewById(R.id.serNameTV);
			holder.btnDel = (ImageView) convertView.findViewById(R.id.btnDel);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.tvServName.setText(servConfig.getServerName());
		holder.btnDel.setOnClickListener(imageClick);
		return convertView;
	}
	private android.view.View.OnClickListener  imageClick=new  View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			alertMsg();
		}
	};
	private OnTouchListener imageButtonTouchListener = new OnTouchListener() {

		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			ImageView btn = (ImageView) v;
			switch (event.getAction()) {

			case MotionEvent.ACTION_DOWN: {
				btn.getDrawable().setAlpha(150);
				btn.invalidate();
				break;
			}
			case MotionEvent.ACTION_MOVE: {
				btn.getDrawable().setAlpha(150);
				btn.invalidate();
				break;
			}
			case MotionEvent.ACTION_UP: {
				btn.getDrawable().setAlpha(255);
				btn.invalidate();
				goTo(btn);
				break;
			}

			default:

				break;
			}

			return false;
		}

	};

	class ViewHolder {
		TextView tvServName;
		ImageView btnDel;
	}
	/**
	 * ɾ��ť�¼�
	 * @param btn
	 */
	protected void goTo(ImageView btn) {
		// TODO Auto-generated method stub
		if(btn.getId()==R.id.btnDel){
			alertMsg();
		}
	}
	private void alertMsg(){
		final AlertDialog dlg=new AlertDialog.Builder(context).create();
		dlg.setTitle("FTP网络助手");
		dlg.setMessage("是否确定删除?");
		dlg.setButton(AlertDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				servList.remove(position);
				notifyDataSetChanged();
			}
		});
		dlg.setButton(AlertDialog.BUTTON_NEGATIVE, "取消",new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
					dlg.dismiss();
			}
		});
		dlg.show();
	}
}
